package com.supermap.authentication.controller;

import com.alibaba.fastjson.JSONObject;
import com.supermap.authentication.service.IAuthenticationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by xiaolu on 2017/10/7.
 */
@RestController
@EnableAutoConfiguration
public class AuthenticationController {
    private Logger logger = LoggerFactory.getLogger(AuthenticationController.class);

    @Resource
    IAuthenticationService authenticationService;

    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject login(@RequestBody JSONObject param) {
        JSONObject result = new JSONObject();
        String methodName = param.getString("methodName");
        JSONObject value = param.getJSONObject("value");

        String data = authenticationService.extractMethod(methodName, new Object[]{value.toJSONString()});
        if (data == null) {
            result.put("status", 0);
        } else {
            result.put("status", 1);
            result.put("data", JSONObject.parseObject(data));
        }
        return result;
    }

    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject login(@RequestParam("methodName") String methodName, @RequestParam("userName") String userName) {
        JSONObject result = new JSONObject();
        try {
            String data = authenticationService.extractMethod(methodName, new Object[]{"{userName:\"" + userName + "\"}"});
            if (data == null) {
                result.put("status", 0);
                result.put("msg", "This user has no data");
            } else {
                result.put("status", 1);
                result.put("msg", "success");
                result.put("data", JSONObject.parseObject(data));
            }
        } catch (Exception ex) {
            result.put("status", -1);
            result.put("msg", "exception");
            logger.error("Get user Info is exception . this exception is : " + ex);
        }
        return result;
    }

    @RequestMapping(value = "getAllForecasters", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getAllForecasters() {
        JSONObject result = new JSONObject();
        String methodName = "getAllProvinceForecaster";
        try {
            String data = authenticationService.extractMethod(methodName, new Object[]{""});
            if (data == null) {
                result.put("status", 0);
                result.put("msg", "Have no data");
            } else {
                result.put("status", 1);
                result.put("msg", "success");
                result.put("data", JSONObject.parseObject(data));
            }
        } catch (Exception ex) {
            result.put("status", -1);
            result.put("msg", "exception");
            logger.error("Get user Info is exception . this exception is : " + ex);
        }
        return result;
    }

}